﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace HypermarketSerialPort._1_Model
{
    class CheckInformationManager
    {
        static private string MSG_PRODUCT_NOT_FOUND = "Sorry, this product currently is not availiable!";

        private static CheckInformationManager mInstance;
        private DBManager mDBManager;

        #region CheckInformationManager
        //only create one instance of CheckInforManager
        public static CheckInformationManager getInstance()
        {
            if (mInstance == null)
            {
                mInstance = new CheckInformationManager();
            }
            return mInstance;
        }
        #endregion


        #region Constructor
        private CheckInformationManager()
        {
            mDBManager = DBManager.getInstance();
        }
        #endregion


        #region Operation Check Product Information
        public string OperationProducrtInfor(List<string> receivedInfoList)
        {
            if (receivedInfoList.Count() == Constant.TWO)
            {
                string productID = receivedInfoList[Constant.ONE];
                string machineID = receivedInfoList[Constant.ZERO];
                return CheckValidProductInfo(productID);
            }
            else
            {
                return Constant.PROGRAM_ERROR;
            }
        }
        #endregion 


        #region Check Valid Product Information
        private string CheckValidProductInfo(string productID)
        {
            DataTable StockProductDiscount = mDBManager.getTable(Constant.STOCK_PRODUCT_DISCOUNT_TABLE);
            string query = Constant.PRODUCT_ID + Constant.EQUAL + Constant.SINGAL_QUOTE + productID +
                    Constant.SINGAL_QUOTE + Constant.AND + Constant.EXPIRE_DATE + Constant.MORE_THAN +
                    Constant.SINGAL_QUOTE + DateTime.Today + Constant.SINGAL_QUOTE;
            DataRow[] discountRows = StockProductDiscount.Select(query);
            if (discountRows.Count() == Constant.MIN_ROW_COUNT)
            {
                //select from stock table only, no discount for this product
                return NoDiscount(productID);
            }
            else
            {
                return HaveDiscount(productID, discountRows.First());
            }
        }

        private string NoDiscount(string productID)
        {
            DataTable Stock = mDBManager.getTable(Constant.STOCK_PRODUCT_TABLE);
            string query = Constant.PRODUCT_ID + Constant.EQUAL + Constant.SINGAL_QUOTE + productID + 
                    Constant.SINGAL_QUOTE + Constant.AND + Constant.EXPIRE_DATE + Constant.MORE_THAN +
                    Constant.SINGAL_QUOTE + DateTime.Today + Constant.SINGAL_QUOTE;
            DataRow[] rows = Stock.Select(query);
            if (rows.Count() == Constant.MIN_QUALITY)
            {
                return MSG_PRODUCT_NOT_FOUND;
            }
            else
            {
                DataRow dr = rows.First();
                string name = dr[Constant.NAME].ToString();
                string sellingPrice = dr[Constant.SELLING_PRICE].ToString();
                string output = Constant.PRODUCT_ID + Constant.COLON + Constant.SPACE + productID + Constant.NEWLINE + Constant.NAME + Constant.COLON + Constant.SPACE
                                + name + Constant.NEWLINE + Constant.SELLING_PRICE + Constant.COLON + Constant.SPACE +
                                Math.Round(Convert.ToDecimal(sellingPrice), Constant.TWO) + Constant.NEWLINE /***sound index****/;
                return output;  
            }
        }

        private static string HaveDiscount(string productID, DataRow dr)
        {
            try
            {
                string name = dr[Constant.NAME].ToString();
                string sellingPrice = dr[Constant.SELLING_PRICE].ToString();
                string freeItermString = string.Empty;
                string discountString = string.Empty;
                if (Convert.ToInt16(dr[Constant.FREE_ITEM_QUANTITY]) > Constant.MIN_DISCOUNT)
                {
                    string freeIterm = dr[Constant.FREE_ITEM_QUANTITY].ToString();
                    freeItermString = Constant.NEWLINE + Constant.FREE_ITEM_QUANTITY + Constant.COLON + freeIterm;
                }
                if (Convert.ToInt16(dr[Constant.DISCOUNT]) > Constant.MIN_DISCOUNT)
                {
                    string discount = dr[Constant.DISCOUNT].ToString();
                    discountString = Constant.NEWLINE + Constant.DISCOUNT + Constant.COLON + Constant.SPACE + discount + Constant.PERCENTAGE;

                }
                string output = Constant.PRODUCT_ID + Constant.COLON + Constant.SPACE + productID + Constant.NEWLINE + Constant.NAME + Constant.COLON + 
                                Constant.SPACE + name + Constant.NEWLINE + Constant.SELLING_PRICE + Constant.COLON + Constant.SPACE +
                                Math.Round(Convert.ToDecimal(sellingPrice), Constant.TWO) + Constant.NEWLINE + Constant.BUNDLE_UNIT + Constant.COLON +
                                Constant.SPACE + dr[Constant.BUNDLE_UNIT] + freeItermString + discountString + Constant.NEWLINE/***sound index****/;
                return output;
            }
            catch
            {
                return Constant.PROGRAM_ERROR;
            }
        }
        #endregion 


    }
}
